<template>
  <div
    v-if="configStore.nav_is_show && navData.length > 0"
    class="x-nav"
    :class="{ short: navData.length < 4, ios: deviceIos, android: !deviceIos }"
  >
    <button
      v-for="x in navData"
      :key="x.id"
      @click="nodeClick(x)"
    >
      <span :style="{ '--nav-icon': `url(${x.icon})`, '--nav-touch-icon': `url(${x.touch_icon || x.icon})` }">
        {{ x.name }}
      </span>
    </button>
    <div class="light"></div>
  </div>
</template>

<script
  lang="ts"
  setup
  name="XNav"
>
import { useSub, useShare, useForm, useMenuTree } from '@/utils/comps'
import { useConfigStore } from '@/store/config'
import { useTtsaStore } from '@/store/ttsa'
import { useAppRouter } from '@/hooks'
import { useTrack } from '@/hooks'

const ttsaStore = useTtsaStore()
const configStore = useConfigStore()
const { setTrack } = useTrack()

const navData = computed(() => configStore.navs)

const deviceIos = computed(() => !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/))

const funMap = {
  '0': (link) => {
    const [content_id, node_id] = link.split('_')
    ttsaStore.next({ content_id, node_id })
  }, // 内容
  '1': useAppRouter, // 外链
  '2': useShare, // 分享
  '3': useSub, // 订阅
  '4': (id) => useForm(id, true), // 表单
  '5': useMenuTree, // 调取目录
}

const nodeClick = (node) => {
  const { link_type, link, interrupt_digital } = node
  if (interrupt_digital && !ttsaStore.ttsa?.paused) ttsaStore.pause()
  if (Number(link_type) === 2) {
    // 统计分享点击
    setTrack({
      name: 'interactive',
      type: 'share',
    })
  }
  funMap[link_type](link)
}

onBeforeMount(() => {
  if (configStore.nav_is_show) {
    configStore.getNavConfig()
  }
})
</script>

<style
  scoped
  lang="scss"
>
.x-nav {
  @apply absolute text-white flex flex-col items-center;
  top: 10px;
  right: 10px;
  width: 50px;
  border-radius: 8px;
  box-sizing: border-box;
  padding: 16px 8px;

  &::after {
    content: '';
    @apply absolute z-0 top-0 left-0 right-0 bottom-0 backdrop-filter backdrop-blur-60;
    background: linear-gradient(-90deg, rgba(76, 190, 255, 0) 0%, rgba(98, 161, 255, 0.55) 100%);
    border-radius: 8px;
    box-shadow: 0px 0px 6px rgba(76, 190, 255, 1) inset;
  }

  &::before {
    content: '';
    @apply absolute z-1 right-0 bottom-0;
    width: 1px;
    height: 54%;
    top: 23%;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0) 5.57%, rgba(255, 255, 255, 1) 37.92%, rgba(0, 0, 0, 0) 100%);
  }

  .light {

    &::after,
    &::before {
      content: '';
      @apply absolute z-1;
    }

    &::before {
      width: 32px;
      height: 28px;
      top: 1px;
      right: 0;
      background: url(../../assets/images/light/highlights-right-top.png) no-repeat top left / 100% 100%;
    }

    &::after {
      width: 43px;
      height: 48.5px;
      bottom: 1px;
      right: 0px;
      background: url(../../assets/images/light/highlights-right-bottom.svg) no-repeat top left / 100% 100%;
    }
  }

  &.android {
    &::after {
      transform: perspective(100px) rotateY(357deg);
    }

    .light {
      &::before {
        top: -2px;
      }

      &::after {
        bottom: -2px;
      }
    }
  }

  &.ios {
    &::after {
      transform: perspective(100px) rotateY(357deg) translateZ(-2px);
    }

    .light {
      &::before {
        top: 1px;
      }

      &::after {
        bottom: 1px;
      }
    }
  }

  &.short {
    .light {
      &::before {
        top: 1.2px;
      }
    }
  }

  button {
    @apply flex flex-col items-center relative z-2;
    width: 100%;
    outline: 0;
    font-size: 8px;
    line-height: 1;
    user-select: none;

    &+button::before {
      content: '';
      @apply block;
      width: 25px;
      height: 1px;
      background-image: linear-gradient(270deg, #4cbeff00 0%, #228ac6a6 46.35%, #4cbeff00 100%);
      margin: 12px 0;
    }

    span {
      display: block;
      width: 100%;
      text-align: center;
      background-position: top center;
      background-repeat: no-repeat;
      background-size: 18px 18px;
      padding-top: 26px;
      background-image: var(--nav-icon);
    }

    &:active span {
      background-image: var(--nav-touch-icon);
      color: #4cbeff;
    }
  }
}

.content-btn {
  @apply absolute bg-cover text-white flc;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 1);
  background: rgba(147, 166, 199, 1);
  top: 36px;
  right: 25px;
  font-size: 18px;
}

.current-name {
  width: 120px;
  height: 15px;
  padding: 0 0 0 2.75px;
  color: white;
  background: transparent;
  font-size: 13px;
}

.tree-modle {
  @apply absolute top-0 left-0 w-screen h-screen z-10086;
}

.modle-container {
  @apply w-full absolute left-0 bottom-0 text-white;
  height: 500px;
  padding-bottom: 34px;
}

.modle-container::after {
  content: '';
  display: block;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  position: absolute;
  top: 0;
  left: 0;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 0;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

.modle-container * {
  position: relative;
  z-index: 1;
}

.modle-title {
  @apply flex items-center justify-between;
  border-bottom: 1px solid rgb(151, 151, 151, 0.6);
  height: 41px;
  padding: 0 4px;
  margin: 0 6px;
  font-size: 14px;
}

.modle-body {
  height: calc(100% - 41px);
}

.content-item {
  display: flex;
  padding: 5px 0;
  margin: 0 12px;
}

.content-item+.content-item {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.content-item img {
  width: 93px;
  height: 70px;
  border-radius: 4px;
  object-fit: cover;
  margin-right: 5px;
}

.content-item p {
  flex: 1;
}

.content-item p b {
  font-size: 14px;
  display: block;
  line-height: 18px;
}

.content-item p span {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  line-height: 14px;
  height: 30px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  margin: 6.5px 0;
}

.content-item p .icon {
  float: right;
  margin-right: 10px;
}
</style>
